package ar.com.cooker.services.uow.recipe;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import ar.com.cooker.dao.RecipeDao;
import ar.com.cooker.dao.StepDao;
import ar.com.cooker.model.Recipe;
import ar.com.cooker.model.Step;
import ar.com.cooker.services.pojo.RecipePojo;
import ar.com.cooker.services.pojo.StepPojo;
import ar.com.cooker.services.uow.GetByIdUOW;
import ar.com.cooker.services.util.ConvertBOtoPojo;

@Component(value= "getRecipeByIdUOW")
public class GetRecipeByIdUOW implements GetByIdUOW<RecipePojo> {

	@Autowired
	private RecipeDao recipeDao;
	
	@Autowired
	private StepDao stepDao;
	
	@Override
	public RecipePojo execute(Integer id) {
		RecipePojo rp = null;
		Recipe r = getRecipeDao().findById(id);
		if(r != null){
			rp = ConvertBOtoPojo.getRecipePojoToRecipe(r);
			List<Step> steps = getStepDao().findStepByRecipe(id);
			StepPojo[] sts = new StepPojo[steps.size()];
			int cant = 0;
			for(Step s : steps){
				sts[cant] = ConvertBOtoPojo.getStepPojoToStep(s);
				cant ++;
			}
			rp.setListSteps(sts);
		}
		return rp;
	}

	public void setRecipeDao(RecipeDao recipeDao) {
		this.recipeDao = recipeDao;
	}

	public RecipeDao getRecipeDao() {
		return recipeDao;
	}

	public void setStepDao(StepDao stepDao) {
		this.stepDao = stepDao;
	}

	public StepDao getStepDao() {
		return stepDao;
	}

}
